package ctl.example.HtmlParseDemo;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.htmlcleaner.TagNode;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class Parser extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button)findViewById(R.id.parse);
        button.setOnClickListener(myListener);
    }
    
    private ProgressDialog pd;   
    private OnClickListener myListener = new OnClickListener() {
        public void onClick(View v) {
            pd = ProgressDialog.show(Parser.this, "Working...", "parsing", true, false);
        	new parseSite().execute("http://www.eva.vn/tinh-yeu-gioi-tinh/phi-cuoi-chong-khong-biet-deo-bao-c3a112563.html");
        }
    };
    
    private class parseSite extends AsyncTask<String, Void, List<String>> {
    	protected List<String> doInBackground(String... arg) {
        	List<String> output = new ArrayList<String>();
        	try
	        {
	        	htmlReader hh = new htmlReader(new URL(arg[0]));
	            List<TagNode> links = hh.getLinksByClass();
	            Log.e(links.iterator()+"", links.iterator().hasNext()+"");
	            for (Iterator<TagNode> iterator = links.iterator(); iterator.hasNext();)
	            {
	                TagNode divElement = (TagNode) iterator.next();
	                output.add(divElement.getText().toString());
	            }
	        }
	        catch(Exception e)
	        {
	            e.printStackTrace();
	        }
        	return output;
        }

        protected void onPostExecute(List<String> output) 
        {

        	pd.dismiss();

        	ListView listview = (ListView) findViewById(R.id.listViewData);

        	listview.setAdapter(new ArrayAdapter<String>(Parser.this,android.R.layout.simple_list_item_1 , output));
        }
    }
}